﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Sinc.ERP.Core.Enum;
namespace Sinc.ERP.Core.Entity;

/// <summary>
/// 序列表
/// </summary>
[SugarTable("Sequence", "序列表")]
public class Sequence : EntityBaseDel
{
    /// <summary>
    /// 序列名称
    /// </summary>
    [Required]
    [SugarColumn(ColumnName = "Name", ColumnDescription = "序列编码", Length = 100)]
    public virtual string Name { get; set; }

    /// <summary>
    /// 序列代码
    /// </summary>
    [Required]
    [SugarColumn(ColumnName = "Code", ColumnDescription = "序列前缀", Length = 100)]
    public virtual string Code { get; set; }

    /// <summary>
    /// 当前值
    /// </summary>
    [Required]
    [SugarColumn(ColumnName = "CurrentValue", ColumnDescription = "当前值")]
    public virtual long CurrentValue { get; set; }

    /// <summary>
    /// 最小值
    /// </summary>
    [Required]
    [SugarColumn(ColumnName = "MinValue", ColumnDescription = "最小值")]
    public virtual long MinValue { get; set; }

    /// <summary>
    /// 最大值
    /// </summary>
    [Required]
    [SugarColumn(ColumnName = "MaxValue", ColumnDescription = "最大值")]
    public virtual long MaxValue { get; set; }

    /// <summary>
    /// 步长
    /// </summary>
    [Required]
    [SugarColumn(ColumnName = "Increment", ColumnDescription = "步长")]
    public virtual long Increment { get; set; }

    [Required]
    [SugarColumn(ColumnName = "Length", ColumnDescription = "序列长度")]
    public virtual int Length { get; set; }

    /// <summary>
    /// 循环模式
    /// </summary>
    [Required]
    [SugarColumn(ColumnName = "Cycle", ColumnDescription = "循环模式")]
    public virtual SequenceCycleTypeEnum Cycle { get; set; }

    /// <summary>
    /// 缓存数量
    /// </summary>
    [Required]
    [SugarColumn(ColumnName = "Cache", ColumnDescription = "缓存数量")]
    public virtual long Cache { get; set; }

    /// <summary>
    /// 备注
    /// </summary>
    [SugarColumn(ColumnName = "Remark", ColumnDescription = "备注", Length = 1000)]
    public virtual string? Remark { get; set; }

    public string GetDateFormat()
    {
        switch (this.Cycle)
        {
            case SequenceCycleTypeEnum.Day:
                return "yyyyMMdd";
            case SequenceCycleTypeEnum.Month:
                return "yyyyMM";
            case SequenceCycleTypeEnum.Year:
                return "yyyy";
        }
        return string.Empty;
    }

    /// <summary>
    /// 是否后面还有值
    /// </summary>
    public bool HasNext() => this.CurrentValue < this.MaxValue;

    #region 获取下一个值
    /// <summary>
    /// 获取下一个值
    /// </summary>
    /// <returns></returns>
    public long GetNextValue()
    {
        if (!this.HasNext())
            throw new IndexOutOfRangeException("当前序列已经没有可用的值!");

        this.CurrentValue += this.Increment;
        return this.CurrentValue;
    }
    #endregion

    #region 是否已经过期
    /// <summary>
    /// 是否已经过期
    /// </summary>
    /// <returns></returns>
    public bool IsExpired()
    {
        var updateTime = this.UpdateTime ?? DateTime.Now;

        // 按天循环
        if (this.Cycle == SequenceCycleTypeEnum.Day && (updateTime.Year != DateTime.Now.Year || updateTime.Month != DateTime.Now.Month || updateTime.Day != DateTime.Now.Day))
            return true;

        // 按月循环
        if (this.Cycle == SequenceCycleTypeEnum.Month && (updateTime.Year != DateTime.Now.Year || updateTime.Month != DateTime.Now.Month))
            return true;

        // 按年循环
        if (this.Cycle == SequenceCycleTypeEnum.Year && updateTime.Year != DateTime.Now.Year)
            return true;

        return false;
    }
    #endregion
}
